﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Windows.Forms;

namespace carApp
{
    public partial class weixiudanlist : Form
    {
        public weixiudanlist()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            weixiudan frm = new weixiudan();
            frm.Show();
        }

        private void weixiudanlist_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadData();
            dtp_time1.Format = DateTimePickerFormat.Custom;
            dtp_time1.CustomFormat = "";
            dtp_time2.Format = DateTimePickerFormat.Custom;
            dtp_time2.CustomFormat = "";
        }
        #region pager
        int pageSize = 30;
        int CurrentPage = 1;
        int pageCount = 0;
        DataTable dt;
        int CurrentRowIndex = 0;
        int CurrentColumnIndex = 0;
        void LoadData()
        {
            string where = "typeid=1";
            if(AppConfig.typeid!="1")
            {
                where = where + " and isdel=0";
            }
            if (txtKeyword.TextLength > 0)
            {
                var keys = db.ReplaceSql(txtKeyword.Text);
                where += " and (danwei like'%" + keys + "%' or sn like'%" + keys + "%')";
            }
            if (txtTime1.TextLength > 0)
            {
                where += " and  times >= #" + txtTime1.Text + " 0:0:0#"; ;
            }
            if (txtTime2.TextLength > 0)
            {
                where += " and  times <= #" + txtTime2.Text + " 23:59:59#"; ;
            }
            if (comboBox1.Text=="未收款")
            {
                where += " and pay_status=0"; ;
            }
            if (comboBox1.Text == "已收款")
            {
                where += " and pay_status=1"; ;
            }
            int RowCount = db.count("vwticket", where);
            decimal amount = db.sum("ticket", "amount", where);
            decimal fee = db.sum("ticket", "fee", where);
            decimal amount_weishou = db.sum("ticket", "amount", where+" and pay_status=0");
            lblTotal.Text = "合计："+amount.ToString("0.00")+" 工时费："+fee.ToString("0.00")+"  未收款："+amount_weishou.ToString("0.00");
            dt = db.GetPagerData("vwticket", "*", where, pageSize, CurrentPage);
            pageCount = (int)Math.Ceiling(RowCount * 1.0 / pageSize);
            lblPageCount.Text = "共 " + pageCount + " 页 / "+ RowCount.ToString()+"条";
            txtPageCurrent.Text = CurrentPage.ToString();
            dataGridView1.DataSource = dt;
        }
        private void lblfirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            LoadData();
        }

        private void lblprev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            if (CurrentPage < 1)
                CurrentPage = 1;
            LoadData();
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            if (CurrentPage > pageCount)
                CurrentPage = pageCount;
            LoadData();
        }

        private void lbllast_Click(object sender, EventArgs e)
        {
            CurrentPage = pageCount;
            LoadData();
        }
        private void btnPageGo_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse(txtPageCurrent.Text);
            LoadData();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex > -1)
            {
                var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
                //string items = db.ExecuteScalar("ticket", "items", "id=" + id);
                //var jarr = JArray.Parse(items);
                //foreach (var arr in jarr)
                //{
                //    var jobj = (JObject)arr;
                //    var goods_id = jobj["id"];
                //    var num = jobj["num"];
                //    if (goods_id.ToString() != "0")
                //    {
                //        db.inc("goods", "id=" + goods_id.ToString(), "stock", int.Parse(num.ToString()));
                //    }
                //}
                if (AppConfig.typeid == "1")
                {
                    db.remove("ticket","id=" + id);
                }
                else
                {
                    Libs.OleDbHelper.ExecuteSql("update ticket set isdel=1 where id=" + id);
                }
                this.dataGridView1.Rows.RemoveAt(CurrentRowIndex);
            }
        }
       
        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex > -1)
            {
                var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value.ToString();
                weixiudan wxfrm = new weixiudan(int.Parse(id));
                wxfrm.ShowDialog();
            }
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            var dgv = (DataGridView)sender;
            CurrentRowIndex = e.RowIndex;
            CurrentColumnIndex = e.ColumnIndex;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && CurrentRowIndex > -1)
            {
                CurrentRowIndex = e.RowIndex;
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
                contextMenuStrip1.Show(Cursor.Position);
            }
        }
        #endregion
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 6)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                if (row != null)
                {
                    if (row.Cells[6].Value.ToString() == "1")
                    {
                        e.Value = string.Format("{0}", "已付款");
                    }
                    else
                    {
                        e.Value = string.Format("{0}", "未付款");
                    }
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要清空吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Libs.OleDbHelper.ExecuteSql("delete from ticket");
                LoadData();
            }
        }

        private void dtp_time1_ValueChanged(object sender, EventArgs e)
        {
            txtTime1.Text = dtp_time1.Value.ToString("yyyy-MM-dd");
        }

        private void dtp_time2_ValueChanged(object sender, EventArgs e)
        {
            txtTime2.Text = dtp_time2.Value.ToString("yyyy-MM-dd");
        }

        private void dtp_time2_CloseUp(object sender, EventArgs e)
        {
            txtTime2.Text = dtp_time2.Value.ToString("yyyy-MM-dd");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            db.remove("ticket");
            LoadData();
        }
    }
}
